package com.panopset.gui;

import static com.panopset.Util.*;
import static com.panopset.gui.LookAndFeel.*;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

/**
 * Standard panel with a white background. Can be titled.
 *
 * @author Karl Dinwiddie
 *
 */
public class TPanel extends JPanel implements WindowConstants {
    TitledBorder titledBorderRequiredDirectory;
    TitledBorder titledBorderRequiredFile;
    TitledBorder titledBorderRequired;
    TitledBorder titledBorder;
    String borderTitle = "";
    Color borderColor = LINE_BORDER_COLOR_NORMAL;

    public static final Insets STANDARD_BORDER_INSETS = new Insets(2, 4, 2, 4);

    public static final LineBorder createLineBorder() {
        return createLineBorder(null, 1);
    }

    public static final LineBorder createLineBorder(Color color, int thickness) {
        return new LineBorder(color == null ? LINE_BORDER_COLOR_NORMAL : color, thickness,
                true) {

            /**
             * Border insets top=2,left=4,bottom=2,right=4.
             *
             * @param Component
             */
            @Override
            public Insets getBorderInsets(Component c) {
                return STANDARD_BORDER_INSETS;
            }

            private static final long serialVersionUID = VERSION_SERIAL_NUMBER;
        };
    }

    private String requiredPrompt;
    public String getRequiredPrompt() {
        if (requiredPrompt == null) {
            requiredPrompt = x("required");
        }
        return requiredPrompt;
    }

    public TitledBorder getTitledBorderRequired() {
        if (titledBorderRequired == null) {
            titledBorderRequired = new TitledBorder(createLineBorder(Color.RED,
                    2), borderTitle + SINGLE_SPACE + OPEN_PAREN + getRequiredPrompt()
                    + CLOSE_PAREN);
            titledBorderRequired.setTitleFont(ARIAL_BOLD_10);
        }
        return titledBorderRequired;
    }

    public TitledBorder getTitledBorder() {
        if (titledBorder == null) {
            titledBorder = new TitledBorder(createLineBorder(borderColor, 1),
                    borderTitle);
            titledBorder.setTitleFont(ARIAL_PLAIN_10);
        }
        return titledBorder;
    }

    public void setNormalBorder() {
        setBorder(getTitledBorder());
    }

    public void setRequiredBorder() {
        setBorder(getTitledBorderRequired());
    }

    void createBorder() {
        setBorder(getTitledBorder());
    }

    public static class Builder {
        String borderTitle;
        String tip;
        String name;
        String requiredPrompt;
        LayoutManager layout;
        Color background;
        Color borderColor;
        final Vector<Component> adds = new Vector<Component>();

        public TPanel construct() {
            TPanel rtn = new TPanel();
            if (name != null) {
                rtn.setName(name);
            }
            if (layout == null) {
                rtn.setLayout(new FlowLayout());
            } else {
                rtn.setLayout(layout);
            }
            if (background != null) {
                rtn.setBackground(background);
            }
            if (tip != null) {
                rtn.setToolTipText(tip);
            }
            for (Component c : adds) {
                rtn.add(c);
            }
            if (borderTitle == null && borderColor == null) {
                return rtn;
            }
            if (borderTitle != null) {
                rtn.borderTitle = borderTitle;
            }
            if (borderColor != null) {
                rtn.borderColor = borderColor;
            }
            if (requiredPrompt != null) {
                rtn.requiredPrompt = requiredPrompt;
            }
            rtn.createBorder();
            return rtn;
        }

        /**
         * Add a component to the panel.
         * @param comp
         * @return
         */
        public Builder add(Component comp) {
            adds.add(comp);
            return this;
        }

        /**
         * Assign a border color. If no title is specified, this method may be
         * used to create a border.
         *
         * @param borderColor
         * @return Builder.
         */
        public Builder borderColor(Color borderColor) {
            this.borderColor = borderColor;
            return this;
        }

        /**
         * Assign a border and title. If no borderColor is specified, default is
         * dark red.
         *
         * @param borderTitle
         * @return
         */
        public Builder borderTitle(String borderTitle) {
            this.borderTitle = borderTitle;
            return this;
        }

        /**
         * Tool tip text.
         *
         * @param tip
         * @return Builder
         */
        public Builder tip(String tip) {
            this.tip = tip;
            return this;
        }

        /**
         * Assign a background color, default is white.
         *
         * @param background
         * @return Builder.
         */
        public Builder background(Color background) {
            this.background = background;
            return this;
        }

        /**
         * If no LayoutManager is specified, GridLayout(1,1) will be used.
         *
         * @param layout
         * @return Builder
         */
        public Builder layout(LayoutManager layout) {
            this.layout = layout;
            return this;
        }

        /**
         * Assign a name to the component, which will show up in a tabbed pane
         * tab.
         *
         * @param name
         * @return Builder.
         */
        public Builder name(String name) {
            this.name = name;
            return this;
        }

        /**
         * Default is &quot;required&quot;.  Change it to something more
         * meaningful with this method.
         * @param requiredPrompt
         * @return Builder.
         */
        public Builder requiredPrompt(String requiredPrompt) {
            this.requiredPrompt = requiredPrompt;
            return this;
        }
    }

    public TPanel() {
        this(new FlowLayout());
    }

    public TPanel(LayoutManager mgr) {
        super(mgr);
        setBackground(BG_COLOR);
    }

    private static final long serialVersionUID = VERSION_SERIAL_NUMBER;
}
